package com.zwy.leecode;

/**
 * @author 天火
 * @date 2019-02-12
 * 解决两个字符串相乘问题
 * 解决方法：
 * 两个字符串的相乘位数最大位两个字符串长度
 * 先求出字符串a乘以字符串b的最后一位，然后将其存入结果字符结果数组中
 *      1 2 3
 *      2 3 4
 *   *_________
 *        1 2
 *      0 8
 *    0 4
 */
public class SolutionFourThree {

    public static void main(String[] args) {
        System.out.println(new Solution().multiply("100121212","92123121"));
    }
}

class Solution{

    public String multiply(String num1,String num2){
        char[] numChar1 = num1.toCharArray();
        char[] numChar2 = num2.toCharArray();
        int[] result = new int[num1.length() + num2.length()];

        for (int i = numChar1.length-1; i>=0;i--){
            for(int j = numChar2.length-1;j>=0; j--){
                //当前的值
                int temp = (num1.charAt(i)-'0') * (num2.charAt(j) - '0');
                temp += result[i+j+1];
                //相乘之后的大于10，需要判断是否进位
                if(temp>=10){
                    result[i+j+1] = (temp%10);
                    result[i+j] += temp/10;
                }else{
                    result[i+j+1] = temp;
                }
            }
        }

        StringBuilder sb = new StringBuilder();
        int i = 0;
        while (result[i]==0 && i<result.length-1){
            i++;
        }
        for(; i<result.length; i++){
                sb.append(result[i]);

        }

        return sb.toString();
    }

}
